"use strict";
Object.defineProperty(exports, "__esModule", {
	value: true
});
exports.request = void 0;
var app = getApp();
console.log(app)
var baseUrl_1 = require("./baseUrl");



var canShowModal = true;
var whiteList = ['/user/login', '/record/handler', '/palm/preBind', '/home/bannersVersion'];
var specialList = [{
	url: '/user/realNameAuth',
	code: '500037'
}];
var request = function(method, url, data) {
	var _params;
	if (method === 'GET') {
		var params = data;
		console.log("=======GET=================", params);
		if (!params || Object.is(params, undefined)) {
			_params = '';
		} else {
			_params = '?';
			for (var key in params) {
				if (params.hasOwnProperty(key) && params[key] !== null) {
					_params += "".concat(key, "=").concat(encodeURIComponent(params[key]), "&");
				}
			}
			url += _params;
		}
	}
	return new Promise(function(resolve, reject) {
		var header = {
			'content-type': 'application/json',
		};
		if (uni.getStorageSync('token')) {
			header.token = uni.getStorageSync('token')
		}
		wx.request({
			url: baseUrl_1.default+url,
			method: method,
			data: method === 'POST' ? JSON.stringify(data) : data,
			header: header,
			success: function(res) {
				console.log(res)
				if (res.statusCode == '200' && res.data.code == '100000000') {
					resolve(res.data);
				} else if (res.data.code == '411000001') {
					//  过期
					let user = uni.getStorageSync('userInfo')
					let password = uni.getStorageSync('password')
					uni.request({
						url: 'https://xingzhiyuan.work:8081/user/login',
						// url:'https://xingzhiyuanst.top/user/login' ,
						method: 'POST',
						data: {
							"userPhone": this.userInfo.userName,
							"password": this.userInfo.password,
							"userType": 2,
							// "code": 100000000,
						},
						success: (res) => {
							console.log(res.data.data)
							this.$store.commit('getToken', res.data.data)
							this.$store.commit('changeName', this.userInfo.userName,
								this.userInfo.password)
							uni.setStorageSync('token', res.data.data)
							uni.setStorageSync('userInfo', this.userInfo.userName)
							uni.setStorageSync('password', this.userInfo.password)

							if (res.data.code !== 100000000) {
								wx.showModal({
									title: "温馨提示",
									content: res.data.msg || '系统异常，请稍后重试',
									confirmColor: '#07C160',
									showCancel: false,
								});
							}
							uni.switchTab({
								url: '/pages/index/index'
							})

						}
					})


					// console.log("重新授权登录1");
					// app.clearStorage();
					// if (canShowModal) {
					//     canShowModal = false;
					//     wx.showModal({
					//         title: '登录',
					//         content: '您好！请先登录',
					//         confirmColor: '#07C160',
					//         success: function (res) {
					//             if (res.confirm) {
					//                uni.navigateTo({
					//                  url:'/pages/login/login/login'
					//                })
					//             }
					//             canShowModal = true;
					//         }
					//     });
					// }
				} else {
					var isWhiteUrl_1 = whiteList.indexOf(url);
					var isSpecial_1 = false;
					specialList.forEach(function(item) {
						var tmpUrl = item.url;
						var tmpCode = item.code;
						if (tmpUrl == url && (tmpCode.indexOf(res.data.code) >= 0 ||
								tmpCode.indexOf(res.data.code) >= 0)) {
							isSpecial_1 = true;
						}
						if (tmpUrl == url) {
							isWhiteUrl_1 = 0;
						}
					});
					if (canShowModal && (isSpecial_1 || isWhiteUrl_1 < 0)) {
						canShowModal = false;
						wx.showModal({
							title: "温馨提示",
							content: res.data.msg || '系统异常，请稍后重试',
							confirmColor: '#07C160',
							showCancel: false,
							success: function() {
								canShowModal = true;
							}
						});
					}
					wx.hideLoading();
					reject(res.data);
				}
			},
			fail: function(err) {
				console.log("failErr", err);
				if (canShowModal) {
					canShowModal = false;
					wx.showModal({
						title: (err.errno == '600003') || (err.errno == '600001') ?
							'网络异常，请重试' : err.errMsg || '服务器异常，请联系管理员',
						confirmColor: '#07C160',
						showCancel: false,
						success: function() {
							canShowModal = true;
						}
					});
				}
				reject(err);
			}
		});
	});
};
exports.request = request;